package com.admin.education.services.Implements;

import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.activity.Mapper.ActivityMapper;
import com.admin.education.dataTransferObject.schools.SchoolsTransfer;
import com.admin.education.dataTransferObject.schools.mapper.SchoolsMapper;
import com.admin.education.dataTransferObject.schools.request.CreateSchoolRequest;
import com.admin.education.dataTransferObject.schools.request.QuerySchoolRequest;
import com.admin.education.dataTransferObject.schools.request.UpdateSchoolRequest;
import com.admin.education.dataTransferObject.sponsor.Mapper.SponsorMapper;
import com.admin.education.dataTransferObject.sponsor.SponsorTransfer;
import com.admin.education.models.Activities;
import com.admin.education.models.QSchools;
import com.admin.education.models.Schools;
import com.admin.education.models.Sponsor;
import com.admin.education.repositories.SchoolsRepository;
import com.admin.education.services.Interfaces.ISchoolService;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
@Service
@Transactional
@Validated
public class SchoolService implements ISchoolService {
    @Autowired
    private SchoolsMapper schoolsMapper;
    private QSchools qSchools;
    @Autowired
    private SchoolsRepository schoolsRepository;
    public  SchoolService()
    {
        qSchools = QSchools.schools;
    }
    @Override
    public ServicesExcuteResult<List<SchoolsTransfer>> querySchools() {
        ServicesExcuteResult<List<SchoolsTransfer>> servicesExcuteResult = new ServicesExcuteResult<>();

        List<Schools>  schools= this.schoolsRepository.selectFrom(qSchools).where(qSchools.valid.eq(true)).fetch();

        servicesExcuteResult.setData(schoolsMapper.mapFrom(schools));
        servicesExcuteResult.setSuccess(true);

        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<PageResponse<SchoolsTransfer>> querySchools(@Valid QuerySchoolRequest querySchoolRequest) {
        ServicesExcuteResult<PageResponse<SchoolsTransfer>> servicesExcuteResult=new ServicesExcuteResult<>();

        JPAQuery<Schools> schoolsJPAQuery = this.schoolsRepository.selectFrom(qSchools);
        if(!StringUtils.isEmpty(querySchoolRequest.getName())){
            schoolsJPAQuery =schoolsJPAQuery.where(qSchools.name.contains(querySchoolRequest.getName()));
        }
        long totalCount = schoolsJPAQuery.fetchCount();

        schoolsJPAQuery = schoolsJPAQuery.orderBy(qSchools.createDate.desc()).offset((querySchoolRequest.getPageIndex()-1)*querySchoolRequest.getPageSize()).limit(querySchoolRequest.getPageSize());

        List<Schools> schools= schoolsJPAQuery.fetch();

        List<SchoolsTransfer> schoolsTransfers = schoolsMapper.mapFrom(schools);

        PageResponse<SchoolsTransfer> schoolsTransferPageResponse = new PageResponse<>(querySchoolRequest.getPageSize(),querySchoolRequest.getPageIndex(),(int)totalCount,schoolsTransfers);

        servicesExcuteResult.setData(schoolsTransferPageResponse);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult createSchools(@Valid CreateSchoolRequest createSchoolRequest) {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        Schools schools = schoolsMapper.mapFrom(createSchoolRequest);
        schools.setCreateDate(new Date());
        schools.setValid(true);

        this.schoolsRepository.persist(schools);

        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult updateSchools(long schoolsId, @Valid UpdateSchoolRequest updateSchoolRequest) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Schools> schoolsJPAQuery = this.schoolsRepository.selectFrom(qSchools).where(qSchools.id.eq(schoolsId));
        if(0!=schoolsJPAQuery.fetchCount()){
            Schools schools = schoolsJPAQuery.fetchFirst();

            schools = schoolsMapper.mapFrom(updateSchoolRequest,schools);

            this.schoolsRepository.merge(schools);

            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("学校不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult updateSchoolsValid(long schoolsId) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Schools> schoolsJPAQuery = this.schoolsRepository.selectFrom(qSchools).where(qSchools.id.eq(schoolsId));
        if(0!=schoolsJPAQuery.fetchCount()){
            Schools schools = schoolsJPAQuery.fetchFirst();

            schools.setValid(!schools.isValid());

            this.schoolsRepository.merge(schools);
            servicesExcuteResult.setSuccess(true);
        }
        else{
            throw new ResourceNotFoundException("学校不存在");
        }
        return  servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult deleteSchool(long schoolsId) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();
        JPAQuery<Schools> schoolsJPAQuery = this.schoolsRepository.selectFrom(qSchools).where(qSchools.id.eq(schoolsId));
        if(0!=schoolsJPAQuery.fetchCount()){
            Schools schools = schoolsJPAQuery.fetchFirst();

            if(!schools.isValid()&&0==schools.getClasses().size()){

                this.schoolsRepository.remove(schools);

                servicesExcuteResult.setSuccess(true);
            }
            else
            {
                servicesExcuteResult.setErrorMessage("学校不能删除");
            }
        }
        else {
            throw new ResourceNotFoundException("学校不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult querySchoolById(long schoolsId) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Schools> schoolsJPAQuery = this.schoolsRepository.selectFrom(qSchools).where(qSchools.id.eq(schoolsId));
        if(0!=schoolsJPAQuery.fetchCount()){
            Schools schools = schoolsJPAQuery.fetchFirst();

            servicesExcuteResult.setData(schoolsMapper.mapFrom(schools));
            servicesExcuteResult.setSuccess(true);
        }
        else{
            throw new ResourceNotFoundException("活动不存在");
        }
        return  servicesExcuteResult;
    }
}
